package com.ctrip.ec.api.mobile.common;

import com.ctrip.ec.api.mobile.filter.AuthFilter;
import com.ctrip.ec.biz.entity.member.MemberEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by cannon on 2018/5/2.
 */
public class RequestContext {

    private  static  ThreadLocal<LoginToken> loginTokenThreadLocal = new ThreadLocal<>();

    private  static  ThreadLocal<ResponseHeader> resHeaderThreadLocal = new ThreadLocal<>();

    private  static  ThreadLocal<Long> agentIdThreadLocal = new ThreadLocal<>();

    private final static Logger logger = LoggerFactory.getLogger(RequestContext.class);

    public  static  void setLoginMember(MemberEntity member){
        LoginToken loginToken = new LoginToken();
        loginToken.setMemberId(member.getId());
        loginToken.setClientIp(member.getLastIp());
        loginToken.setLevel(member.getLevel());
       // loginToken.setAgentId(member.getAgentId());
        loginTokenThreadLocal.set(loginToken);

     //   logger.info("setLoginMember sucess!");

    }

    public  static  void clearLoginToken(){
        loginTokenThreadLocal.remove();
    }

    public  static  Long getMemberId(){
        if(loginTokenThreadLocal.get() == null){
            return null;
        }
        else {
            return loginTokenThreadLocal.get().getMemberId();
        }
    }
    public  static  Integer getMemberLevel(){
        if(loginTokenThreadLocal.get() == null){
            return null;
        }
        else {
            return loginTokenThreadLocal.get().getLevel();
        }
    }

    public  static  String getClientIp(){
        if(loginTokenThreadLocal.get() == null)
            return null;
        else
            return loginTokenThreadLocal.get().getClientIp();
    }

    public  static  Long getAgentId(){

        if(agentIdThreadLocal.get() == null){
            return 0L;
        }
        else {
            return agentIdThreadLocal.get();
        }
    }
    public  static  void setAgentId(Long agentId){
        agentIdThreadLocal.set(agentId);
    }

    public  static  void setResponseHeaderPager(PagerInfo pager){
        createHeader();
        resHeaderThreadLocal.get().setPager(pager);
    }

    public  static  void setResponseHeaderMessage(String message){
        createHeader();
        resHeaderThreadLocal.get().setMessage(message);
    }

    public static  void setResponseHeaderStatus(ResponseStatus status){
        createHeader();
        resHeaderThreadLocal.get().setStatus(status.getId());
    }

    public  static  void clearResponseHeader(){
        resHeaderThreadLocal.remove();
    }

    public  static PagerInfo getResponseHeaderPager(){
        ResponseHeader header = resHeaderThreadLocal.get();
        if(header != null){
            return header.getPager();
        }
        else
        {
            return null;
        }
    }

    public  static  String getResponseHeaderMessage(){
        ResponseHeader header = resHeaderThreadLocal.get();
        if(header != null){
            return header.getMessage();
        }
        else
        {
            return null;
        }
    }

    public static  Integer getResponseHeaderStatus(){
        ResponseHeader header = resHeaderThreadLocal.get();
        if(header != null){
            return header.getStatus();
        }
        else
        {
            return null;
        }
    }

    private    static void createHeader(){
        if(resHeaderThreadLocal.get() == null){
            resHeaderThreadLocal.set(new ResponseHeader());
        }
    }


    private static class  LoginToken{
        private  long memberId;
        private  String clientIp;
        private  Integer level;
       // private  long agentId;

//        public long getAgentId() {
//            return agentId;
//        }
//
//        public void setAgentId(long agentId) {
//            this.agentId = agentId;
//        }

        public Integer getLevel() {
            return level;
        }

        public void setLevel(Integer level) {
            this.level = level;
        }

        public long getMemberId() {
            return memberId;
        }

        public void setMemberId(long memberId) {
            this.memberId = memberId;
        }

        public String getClientIp() {
            return clientIp;
        }

        public void setClientIp(String clientIp) {
            this.clientIp = clientIp;
        }



    }

    private static  class  ResponseHeader{
        private  Integer status;
        private PagerInfo pager;
        private  String message;

        public Integer getStatus() {
            return status;
        }

        public void setStatus(Integer status) {
            this.status = status;
        }

        public PagerInfo getPager() {
            return pager;
        }

        public void setPager(PagerInfo pager) {
            this.pager = pager;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }



}
